<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Base extends Model
{
    use HasFactory;
    use SoftDeletes;

    //拒绝不添加的字段
    protected $guarded=[];

    /**
     * 递归
     * @param $data 传入的参数
     * @param int $pid 父级id
     * @param string $html 添加的分隔符
     * @param int $level  层级
     */
    public function treelevel($data,$pid=0,$html='--',$level=0)
    {
        //定义静态数组 所以无法覆盖
        static $arr = [];

        foreach ($data as $val){
            if($val['pid'] == $pid){
                $val['html'] = str_repeat($html,$level*2);
                $val['level'] = $level+1;
                $arr[] = $val;
                $this->treelevel($data,$val['id'],$html,$val['level']);
            }
        }
//        print_r($arr);
        return $arr;
    }
}
